<template>
  <div class="i-table-no-border">
    <div>
      <table-btns
        :tableTopOperateData="listData.tableTopOperate"
        :tableTopBatchOperate="listData.tableTopBatchOperate"
        :selectedData="selectedData"
        :requestApi="listData.requestApi"
        @pSub="pSub"
        :basePath="basePath"
        @exportData="exportData"
        @actionsChange="getData"
      >
      </table-btns>
    </div>
    <!-- 表格内容 start  -->
    <Table
      ref="table"
      max-height="450"
      :columns="columns"
      :data="tableDataList"
      :loading="loading"
      class="ivu-mt"
      stripe
      @on-filter-change="handleFilterChange"
      @on-select="handleSelect"
      @on-select-cancel="handleSelectCancel"
      @on-select-all="handleSelectAll"
      @on-select-all-cancel="handleSelectAllCancel"
    >
      <template slot-scope="{ row }" slot="salePriceTypeUpdate">
        <span>{{ formatSalePriceType(row.salePriceTypeUpdate) }}</span>
      </template>
    </Table>
    <!-- 表格内容 end  -->
    <!-- 分页 start  -->
  </div>
</template>
<script>
import { mapState, mapMutations } from 'vuex';
import util from '@/libs/util';
import { ApiRequest } from '@api/basePage';
import tables from '@/mixins/dynamictable'; // 混入通用代码

import qwInput from '@/components/common/formItem/qwInput'; // 表单input组件
import qwSelect from '@/components/common/formItem/qwSelect'; // 表单select组件
import qwDatePicker from '@/components/common/formItem/qwDatePicker'; // 表单日期组件
import qwSelectTree from '@/components/common/formItem/qwSelectTree'; // 表单下拉树组件
import tableBtns from './tableBtns'; // 表格内部操作按钮
import { SALE_PRICE_TYPE } from '../../constans';

export default {
  mixins: [tables],
  props: {
    dynamicData: {
      type: Object
    },
    pagePath: {
      type: String
    },
    orderState: {
      type: String,
      default: ''
    }
  },
  computed: {
    ...mapState('admin/baseList', [
      'listData',
      'searchFormData',
      'searchFormDataQ'
    ])
  },
  watch: {
    orderState (n, o) {
      this.getData();
    }
  },
  components: {
    qwInput,
    qwSelect,
    qwDatePicker,
    qwSelectTree,
    tableBtns
  },
  data () {
    return {
      salePriceTypes: SALE_PRICE_TYPE,
      actions: [],
      specTableData: [],
      util: util,
      recordNo: '',
      totalNum: 0,
      pageShow: 10, // 每页展示条数
      nowPage: 1, // 当前页码
      totalPage: 0, // 数据总条数
      markupTypeUpdateList: [
        {
          optionValue: '1',
          optionHtml: '金额'
        },
        {
          optionValue: '2',
          optionHtml: '比例'
        }
      ],
      comparePirceTypeUpdateList: [
        {
          optionValue: '1',
          optionHtml: '售价'
        },
        {
          optionValue: '2',
          optionHtml: '现金价'
        },
        {
          optionValue: '3',
          optionHtml: '分销价'
        }
      ],
      compareTypeUpdateList: [
        {
          optionValue: '1',
          optionHtml: '一品多供'
        },
        {
          optionValue: '2',
          optionHtml: '竞争对手'
        },
        {
          optionValue: '3',
          optionHtml: '同品类供应商'
        },
        {
          optionValue: '4',
          optionHtml: '同品牌供应商'
        }
      ],
      basePath: this.dynamicData.basePath, // 拼接基础路径
      tableDataList: [],
      select: [
        {
          label: '一品多供',
          value: '0'
        },
        {
          label: '同品牌供应商',
          value: '1'
        },
        {
          label: '同类供应商',
          value: '2'
        },
        {
          label: '竞争对手',
          value: '3'
        }
      ],
      compareModal: false,
      selectIndex: 0,
      compareData: [],
      compareCol: [
        {
          title: '比价对象',
          key: 'compareTargetUpdate',
          align: 'center'
        },
        {
          title: '比价对象名字',
          key: 'compareTargetNameUpdate',
          align: 'center'
        },
        {
          title: '联系方式',
          key: 'compareTelUpdate',
          align: 'center'
        },
        {
          title: '地址',
          key: 'compareAddressUpdate',
          align: 'center'
        },
        {
          title: '操作',
          slot: 'action',
          align: 'center',
          fixed: 'right',
          minWidth: 120
        }
      ]
    };
  },
  created () {},
  methods: {
    ...mapMutations('admin/baseList', ['setTableRowData']),
    compareFun (row, index) {
      this.compareModal = true;
      this.selectIndex = index;
    },
    // 选中后处理
    compareOk (row) {
      this.tableDataList[this.selectIndex].compareTargetUpdate =
        row.compareTargetUpdate;
      this.tableDataList[this.selectIndex].compareTelUpdate =
        row.compareTelUpdate;
      this.tableDataList[this.selectIndex].compareAddressUpdate =
        row.compareAddressUpdate;
      this.tableDataList[this.selectIndex].compareTargetNameUpdate =
        row.compareTargetNameUpdate;
      this.tableDataList[this.selectIndex].compareManUpdate =
        row.compareManUpdate;
      this.compareModal = false;
    },
    compareTypeSelect (type, row) {},
    // 获取表格的数据
    getData () {
      this.recordNo = this.$route.query.recordNo;
      ApiRequest(
        '/sysback/salepriceruleupdate/getSalePriceRuleByRecordNo' +
          '?recordNo=' +
          this.recordNo,
        'post'
      ).then((res) => {
        if (res.retStatus === '1') {
          this.tableDataList = [res.retData];
        }
      });
    },
    changInput (row, index) {
      this.tableDataList[index] = row;
    },
    // 批量提交按钮
    pSub () {
      if (this.tableDataList.length > 0) {
        ApiRequest(
          '/sysback/salepriceruleupdate/batchUpdateRule',
          'post',
          this.tableDataList
        ).then((res) => {
          if (res.retStatus == '1') {
            this.$Message.success('提交成功');
            this.getData();
          }
        });
      }
    },
    // 切换分页
    handleChangePage (val) {
      this.pageShow = val;
      this.$nextTick(() => {
        this.getData();
      });
    },
    handleClickMore (name, row) {
      if (name === 'Delete') {
        this.$Modal.confirm({
          title: '删除操作',
          content: '确定删除此数据吗？',
          onOk: () => {
            let data = {
              uuid: row.uuid
            };
            // 开始请求商品列表
            ApiRequest(
              this.requestApi.delTableList.url,
              this.requestApi.delTableList.type,
              data
            ).then((res) => {
              this.$Message.success('删除成功');
              this.actionsChange();
            });
          }
        });
      } else {
        let obj = {}; // 动作的对象信息
        row.actions.forEach((item) => {
          if (name === item.name) {
            obj = item;
            return false;
          }
        });
        this.setTableRowData(row);
        if (obj.handleType === 'link') {
          // 操作类型为链接时，拼接参数跳转
          let jumpPath = this.basePath + obj.name;
          this.$router.push({
            path: jumpPath,
            query: {
              uuid: row.uuid,
              orderType: row.orderType
            }
          });
        } else if (obj.handleType === 'prop') {
          // 操作类型为弹窗时 打开弹窗
          if (obj.name === 'viewing') {
            this.showPropviewing = true;
          } else if (obj.name === 'view') {
            this.showPropviewing = true;
          } else if (obj.name === 'Sub') {
            let datas = {
              recordNo: row.recordNo,
              categoryUuid: row.categoryUuid,
              oldUuid: row.oldUuid,
              skuNo: row.skuNo,
              spuNo: row.spuNo,
              productName: row.old.productName,
              productManagerArea: row.old.productManagerArea,
              unit: row.old.unit,
              spec: row.old.spec,
              productUuid: row.old.productUuid,
              storeUuidUpdate: row.storeUuidUpdate,
              storeNoUpdate: row.storeNoUpdate,
              storeNameUpdate: row.storeNameUpdate,
              compareAreaUpdate: row.compareAreaUpdate,
              compareTypeUpdate: row.compareTypeUpdate,
              compareTargetNameUpdate: row.compareTargetNameUpdate,
              compareTargetUpdate: row.compareTargetUpdate,
              markupTypeUpdate: row.markupTypeUpdate,
              markupValueUpdate: row.markupValueUpdate,
              comparePirceTypeUpdate: row.comparePirceTypeUpdate,
              weighMarkupValueUpdate: row.weighMarkupValueUpdate,
              salePriceTypeUpdate: row.salePriceTypeUpdate,
              compareTelUpdate: row.compareTelUpdate,
              compareManUpdate: row.compareManUpdate,
              compareAddressUpdate: row.compareAddressUpdate,
              brandDiffUpdate: row.brandDiffUpdate,
              lastSaleCountUpdate: row.lastSaleCountUpdate,
              nowSaleCountUpdate: row.nowSaleCountUpdate,
              saleRateUpdate: row.saleRateUpdate,
              compareRuleUpdate: row.compareRuleUpdate,
              securityRateMaxUpdate: row.securityRateMaxUpdate,
              securityRateMinUpdate: row.securityRateMinUpdate,
              securityPriceMinUpdate: row.securityPriceMinUpdate,
              securityPriceMaxUpdate: row.securityPriceMaxUpdate
            };
            ApiRequest(
              this.listData.requestApi.subTableList.url,
              this.listData.requestApi.subTableList.type,
              datas
            ).then((res) => {
              if (res.retStatus == '1') {
                this.$Message.success('操作成功！');
                let jumpPath = this.basePath + 'Startuse';
                this.$router.push({
                  path: jumpPath
                });
              }
            });
          } else {
          }
        } else {
          // 操作类型为弹窗时 打开弹窗
        }
      }
    },
    formatSalePriceType (typeVal) {
      const typeArr = SALE_PRICE_TYPE.filter((item) => item.value == typeVal);
      if (typeArr.length !== 0) {
        return typeArr[0].name;
      }
      return typeVal;
    }
  }
};
</script>
<style lang="less" scoped>
.i-table-no-border .ivu-table-header th {
  background: #f8f8f8;
}
</style>
